package com.video.video.service.impl;

import com.video.common.exception.ServiceException;
import com.video.common.utils.StringUtils;
import com.video.common.utils.uuid.IdUtils;
import com.video.video.domain.DGoods;
import com.video.video.domain.GoodDownloadRecord;
import com.video.video.mapper.GoodDownloadRecordMapper;
import com.video.video.service.IDGoodsService;
import com.video.video.service.IGoodDownloadRecordService;
import java.util.List;
import com.video.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * vip下载记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-16
 */
@Service
public class GoodDownloadRecordServiceImpl implements IGoodDownloadRecordService
{
    @Autowired
    private GoodDownloadRecordMapper goodDownloadRecordMapper;
    @Autowired
    private IDGoodsService idGoodsService;

    @Override
    public int getVipDownloadNum(String uid) {
        GoodDownloadRecord goodDownloadRecord=new GoodDownloadRecord();
        goodDownloadRecord.setUid(uid);
        goodDownloadRecord.setCreateTime(DateUtils.getNowDate());
        return goodDownloadRecordMapper.getVipDownloadNum(goodDownloadRecord);
    }

    /**
     * 查询vip下载记录
     *
     * @param id vip下载记录主键
     * @return vip下载记录
     */
    @Override
    public GoodDownloadRecord selectGoodDownloadRecordById(String id)
    {
        return goodDownloadRecordMapper.selectGoodDownloadRecordById(id);
    }

    /**
     * 查询vip下载记录列表
     *
     * @param goodDownloadRecord vip下载记录
     * @return vip下载记录
     */
    @Override
    public List<GoodDownloadRecord> selectGoodDownloadRecordList(GoodDownloadRecord goodDownloadRecord)
    {
        return goodDownloadRecordMapper.selectGoodDownloadRecordList(goodDownloadRecord);
    }

    /**
     * 新增vip下载记录
     *
     * @param goodDownloadRecord vip下载记录
     * @return 结果
     */
    @Override
    public int insertGoodDownloadRecord(GoodDownloadRecord goodDownloadRecord)
    {
        DGoods goods= idGoodsService.selectDGoodsById(goodDownloadRecord.getGid());
        if(goods == null){
            throw new ServiceException("商品不存在");
        }
        if(goods.getStatus().equals("2")){
            throw new ServiceException("商品已下架");
        }
        if(StringUtils.isNotEmpty(goods.getBuyerId())){
            throw new ServiceException("商品已出售");
        }

        //修改商品购买人
        goods.setBuyerId(goodDownloadRecord.getUid());
        idGoodsService.updateDGoods(goods);


        goodDownloadRecord.setId(IdUtils.getId());
        goodDownloadRecord.setGoodName(goods.getGoodName());
        goodDownloadRecord.setImageUrl(goods.getImageUrl());
        goodDownloadRecord.setVideoUrl(goods.getVideoUrl());
        goodDownloadRecord.setCreateBy(goodDownloadRecord.getUid());
        goodDownloadRecord.setCreateTime(DateUtils.getNowDate());
        return goodDownloadRecordMapper.insertGoodDownloadRecord(goodDownloadRecord);
    }

    /**
     * 修改vip下载记录
     *
     * @param goodDownloadRecord vip下载记录
     * @return 结果
     */
    @Override
    public int updateGoodDownloadRecord(GoodDownloadRecord goodDownloadRecord)
    {
        goodDownloadRecord.setUpdateTime(DateUtils.getNowDate());
        return goodDownloadRecordMapper.updateGoodDownloadRecord(goodDownloadRecord);
    }

    /**
     * 批量删除vip下载记录
     *
     * @param ids 需要删除的vip下载记录主键
     * @return 结果
     */
    @Override
    public int deleteGoodDownloadRecordByIds(String[] ids)
    {
        return goodDownloadRecordMapper.deleteGoodDownloadRecordByIds(ids);
    }

    /**
     * 删除vip下载记录信息
     *
     * @param id vip下载记录主键
     * @return 结果
     */
    @Override
    public int deleteGoodDownloadRecordById(String id)
    {
        return goodDownloadRecordMapper.deleteGoodDownloadRecordById(id);
    }
}
